﻿using ModernShopping.AppCode;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.User
{
    public partial class GoogleMapUpdate : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.GetLoginInfo() != null)
                {
                    LoginInfo loginInfo = Request.GetLoginInfo();
                    Session.SetLoginInfo(loginInfo);
                }
            }
            base.OnInit(e);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                List<Store> stores = new List<Store>();
                if (Request.QueryString["SelectedStore"] != null)
                {
                    int ID = int.Parse(Request.QueryString["SelectedStore"]);
                    stores.Add(DbStore.GetByID(ID));
                }
                if (stores.Count > 0)
                {
                    string displayMarkers = "setMarkerFromDatabase('{0}');";
                    displayMarkers = string.Format(displayMarkers, Marker.ParseListJson(stores));
                    this.Page.ClientScript.RegisterStartupScript(this.GetType(), "Create", displayMarkers, true);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Countdown.ShowPopup();
            LoginInfo loginInfo = Session.GetLoginInfo();
            string googlemapData = markerObjects.Value;
            List<Marker> listMarker = JsonConvert.DeserializeObject<List<Marker>>(googlemapData);
            listMarker = listMarker.Where(c => c.Command != Marker.NONE).ToList<Marker>();
            if (listMarker.Count > 0)
            {
                if (DbStore.HandleMarker(listMarker, loginInfo.UserID))
                {
                    Countdown.InitCountdown(2, "Đã lưu thành công");
                    Countdown.Start();
                }
            }
            else
            {
                Countdown.HidePopup();
                MyPopup.InitPopup("Không có bất kì sự thay đổi nào", "<Br/>Vui lòng kiểm tra lại", "red");
                MyPopup.Show();
            }
        }
    }
}